using Identity.Application.ApiPermissionBindings.Commands;
using Identity.Application.Contracts.Requests.PermissionBindings;
using Identity.Application.PermissionKeyResourceIds.Commands;
using Identity.Application.PermissionKeyResourceIds.Queries;
using Identity.Application.Permissions.Commands;
using Identity.HttpAPI.Permission;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Identity.HttpAPI.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api/permissionbindings")]
    public class PermissionBingsController : ControllerBase
    {
        private readonly IMediator _mediator;
        public PermissionBingsController(IMediator mediator)
        {
            _mediator = mediator;
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        [Permission("permissionbinding:item:assign", "权限绑定")]
        public async Task<IActionResult> PermissionBindingAsync([FromBody] AssignPermissionRequest req)
        {
            var command = new AssignPermissionCommand(req.resourceId, req.permissionId);
            var result = await _mediator.Send(command);

            return Ok(result);
        }

        [HttpDelete("{resourceId}")]
        [Authorize(Policy = "permissionbinding:item:delete")]
        [Permission("permissionbinding:item:delete", "删除权限绑定")]
        public async Task<IActionResult> DeletePermissionBindingAsync(string resourceId)
        {
            var command = new DeletePermissionCommand(Guid.Parse(resourceId));
            await _mediator.Send(command);

            return NoContent();
        }

        [HttpGet]
        [Authorize(Roles = "Admin")]
        [Permission("permissionbinding:item:view", "查看权限绑定")]
        public async Task<IActionResult> GetPermissionBindingsAsync()
        {
            var command = new GetAllPermissionKeyResourceIdQuery();
            var result = await _mediator.Send(command);
            return Ok(result);

        }

        [HttpPatch("isActive")]
        [Authorize(Roles = "Admin")]
        [Permission("permissionbinding:item:isActive:update", "启用/禁用接口")]
        public async Task<IActionResult> ChangeApiIsActiveAsync([FromBody] ChangeApiIsActiveRequest req)
        {
            var command = new ChangApiIsActiveCommand(req.resourceId, req.isActive);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPatch("Update")]
        [Authorize(Roles = "Admin")]
        [Permission("permissionbinding:item:Permission:update", "更新接口绑定权限")]
        public async Task<IActionResult> ChangePermissionAsync([FromBody] ChangeApiPermissionBindingRequest req)
        {
            var command = new ChangeApiPermissionBindingCommand(req.resourceId, req.permissionId);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPatch("scope")]
        [Authorize(Roles = "Admin")]
        [Permission("apipermissionbinding:scope:update", "更新接口认证方式")]
        public async Task<IActionResult> ChangePermissionScopeAsync([FromBody] ChangeApiPermissionScopeRequest req)
        {
            var command = new ChangeApiPermissionScopeCommand(req.resourceId, req.PermissionScopeValue);
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    }
}